package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.Shop;
import com.example.shengyin.service.ShopService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Validated
//@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
@Api("门店") // swagger 文档生成
public class ShopController {

    @Autowired
    private ShopService service;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Result list(Page page,@ApiParam(name = "sname",value = "门店名") @RequestParam(required = false) String sname,HttpServletRequest request)
    {

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        List<Shop> list = service.list(page,sname);
        int count = service.count(sname);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page,"成功");
    }

    @ApiOperation("添加地区")
    @PostMapping("/add")
    public Result insert(@Valid Shop shop, BindingResult bindingResult, HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        int insert = service.insert(shop);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }



    @GetMapping("/selectShopById/{id}")
    public Result selectShopById(@PathVariable(value = "id") Long id, HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        Shop shop = service.selectShopById(id);
        System.out.println(shop);
        return shop==null?Result.error("没有对象"):Result.success(shop);
    }


    // 根据cityid返回所有的店
    @GetMapping("/select/{id}")
    public Result select(@PathVariable(value = "id") Long id, HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        List<Shop> select = service.select(id);
        System.out.println(JSON.toJSONString(select));
        return select==null?Result.error("无数据"):Result.success(select);
    }
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Result edit(@Valid Shop shop, HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        int insert = service.update(shop);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @ApiOperation("删除单个")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")int id, HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @ApiOperation("删除多个")
    @DeleteMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids, HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
}
